# 定时任务
import asyncio

from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
from apscheduler.schedulers.asyncio import AsyncIOScheduler

from task import download_file, video_requests


# 定时任务
def scheduled_task():
    print("Running scheduled task...")


executors = {
    'default': ThreadPoolExecutor(10),  # 默认线程池大小
    'processpool': ProcessPoolExecutor(5)  # 进程池大小
}

job_defaults = {
    'coalesce': False,
    'max_instances': 3  # 最大并发实例数
}

# 初始化调度器,异步任务
scheduler = AsyncIOScheduler(executors=executors, job_defaults=job_defaults)

def configure_scheduler():
    # 获取全部数据
    # scheduler.add_job(lambda: asyncio.run(video_requests.get_all_video_list()), 'interval', seconds=3600 * 12)
    # 获取各个分类下的第一页数据
    # 每天早上5点采集最新视频
    scheduler.add_job(lambda: video_requests.get_new_video_list(), 'cron', hour=5, minute=0)
    # scheduler.add_job(lambda: testJob(), 'interval', seconds=10)


def start_scheduler(app):
    global scheduler
    configure_scheduler()
    scheduler.start()


def stop_scheduler(app):
    global scheduler
    scheduler.shutdown()


def testJob():
    print("testJob")
